using System;
using System.Collections.Generic;
using Light;

namespace Refresh
{
    public static class RefreshDataPool<T> where T : RefreshData<T>, new()
    {
        private static readonly List<T> pool = new();

        public static T Get()
        {
            lock (pool) // ʹ������ȷ���̰߳�ȫ��������Ƕ��̻߳����Ļ���  
            {
                if (pool.Count > 0)
                {
                    // �����������Ƿ����б��е����һ�����󣬲��Ƴ����Թ�����  
                    // ע�⣺����ܻᵼ�¶�������˳������⣬��������Ҫ����ʵ���������  
                    var obj = pool[pool.Count - 1];
                    pool.RemoveAt(pool.Count - 1);
                    return obj;
                }

                // ���û�п��õĶ����򴴽�һ���µĶ���  
                return new T();
            }
        }

        public static void Push(T obj)
        {
            pool.Add(obj);
        }
    }

    public interface IRefresh
    {
        /// <summary>
        ///     ҳ������ˢ��
        /// </summary>
        void Refresh<T>(T data) where T : RefreshData<T>, new();
    }

    public abstract class RefreshDataBase
    {
    }

    public class RefreshData<T> : RefreshDataBase, IDisposable where T : RefreshData<T>, new()
    {
        /// <summary>
        ///     ʹ����ɺ�ǵ��ͷ�
        /// </summary>
        public void Dispose()
        {
            RefreshDataPool<T>.Push((T)this);
        }

        /// <summary>
        ///     ��ȡ�¼�
        /// </summary>
        /// <returns></returns>
        public static T Get()
        {
            return RefreshDataPool<T>.Get();
        }
    }

    /// <summary>
    ///     ��̨��ǰ̨UI����������
    /// </summary>
    public class StatusBarInfo : RefreshData<StatusBarInfo>
    {
        public FunctionBarContentType contentType;

        //��Ҫ���������ݽ��еĵ���
        public List<(FunctionBarData, FunctionBarOperationType)> operations = new();
        public FunctionBarType type;
    }

    /// <summary>
    ///     Item�б�ˢ��
    /// </summary>
    public class ItemRefreshData : RefreshData<ItemRefreshData>
    {
        public AbstractItemSO data;
        public FunctionBarOperationType type;
    }

    /// <summary>
    ///     Item��ϸ����ˢ��
    /// </summary>
    public class ItemDetailRefreshData : RefreshData<ItemDetailRefreshData>
    {
        public AbstractItemSO data;
    }
}